package com.example.http.demo.service.impl;

import com.example.http.demo.config.HttpDemoProperties;
import com.example.http.demo.dto.LoginUserDTO;
import com.example.http.demo.dto.RegisterUserDTO;
import com.example.http.demo.dto.UpdateUserDTO;
import com.example.http.demo.model.TblApiKey;
import com.example.http.demo.reponse.HttpBaseResponse;
import com.example.http.demo.service.ApiKeyService;
import com.example.http.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HttpDemoProperties httpDemoProperties;

    @Autowired
    private ApiKeyService apiKeyService;

    @Override
    public void registerUser(RegisterUserDTO userDTO) {
        //数据库已经存在api_key
        TblApiKey tblApiKey = apiKeyService.selectApiKey();
        if (tblApiKey == null) {
            return;
        }
        String apiKey = tblApiKey.getApiKey();
        //1.请求三方注册的接口
        String requestUrl = httpDemoProperties.getRequestBaseUrl() + httpDemoProperties.getUserRegisterUrl();
        ResponseEntity<HttpBaseResponse> baseResponseResponseEntity = this.restTemplate.postForEntity(requestUrl, null, HttpBaseResponse.class,
                userDTO.getName(), userDTO.getPasswd(), apiKey);
        //2.保存请求
        //3.保存响应
        System.out.println("响应：" + baseResponseResponseEntity.getBody());
    }

    @Override
    public void loginUser(LoginUserDTO userDTO) {
        //数据库已经存在api_key
        TblApiKey tblApiKey = apiKeyService.selectApiKey();
        if (tblApiKey == null) {
            return;
        }
        String apiKey = tblApiKey.getApiKey();
        //1.请求三方注册的接口
        String requestUrl = httpDemoProperties.getRequestBaseUrl() + httpDemoProperties.getUserLoginUrl();
        ResponseEntity<HttpBaseResponse> baseResponseResponseEntity = this.restTemplate.postForEntity(requestUrl, null, HttpBaseResponse.class,
                userDTO.getName(), userDTO.getPasswd(), apiKey);
        //2.保存请求
        //3.保存响应
        System.out.println("响应：" + baseResponseResponseEntity.getBody());
    }

    @Override
    public void updateUser(UpdateUserDTO userDTO) {
        //数据库已经存在api_key
        TblApiKey tblApiKey = apiKeyService.selectApiKey();
        if (tblApiKey == null) {
            return;
        }
        String apiKey = tblApiKey.getApiKey();
        //1.请求三方注册的接口
        String requestUrl = httpDemoProperties.getRequestBaseUrl() + httpDemoProperties.getUserInfoUpdateUrl();
        ResponseEntity<HttpBaseResponse> baseResponseResponseEntity = this.restTemplate.postForEntity(requestUrl, null, HttpBaseResponse.class,
                userDTO.getName(), userDTO.getPasswd(), apiKey, userDTO.getEmail());
        //2.保存请求
        //3.保存响应
        System.out.println("响应：" + baseResponseResponseEntity.getBody());
    }

}
